package disruptor;

import com.lmax.disruptor.SleepingWaitStrategy;
import com.lmax.disruptor.WorkHandler;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.Executors;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-12-22 22:36
 **/

/**
 * 多消费者不重复消费
 */
public class Disruptor_WorkHandler implements WorkHandler<DisruptorEvent> {

    @Override
    public void onEvent(DisruptorEvent disruptorEvent) throws Exception {
        System.err.println(disruptorEvent.toString());
    }
}


class TestWorkHandler {


    public static void main(String[] args) {
        DisruptorEventFactory factory = new DisruptorEventFactory();

        // Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;
        // Construct the Disruptor
        Disruptor<DisruptorEvent> disruptor = new Disruptor<>(factory, bufferSize, Executors.defaultThreadFactory(),
                ProducerType.MULTI, new SleepingWaitStrategy());

        Disruptor_WorkHandler[] consumers = new Disruptor_WorkHandler[10];
        for (int i = 0; i < consumers.length; i++) {
            consumers[i] = new Disruptor_WorkHandler();
        }
        disruptor.handleEventsWithWorkerPool(consumers);
        disruptor.start();
        /**
         * 发布事件
         */
        disruptor.publishEvent(((disruptorEvent, Long) -> disruptorEvent.set(1L)));
        disruptor.publishEvent(((disruptorEvent, Long) -> disruptorEvent.set(1L)));
        disruptor.publishEvent(((disruptorEvent, Long) -> disruptorEvent.set(1L)));

    }

}